package com.yy.launchercaijing

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.text.TextUtils
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.pgyersdk.update.PgyUpdateManager

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        enableFullScreen()


        findViewById<ImageView>(R.id.imgv_top).setOnClickListener{
            startApp("uni.UNIEFBD368","io.dcloud.PandoraEntry")
        }


        findViewById<TextView>(R.id.tv_1).setOnClickListener{
            startApp("huh.ZXQO770GY82.szr","io.dcloud.PandoraEntry")
        }

        findViewById<TextView>(R.id.tv_2).setOnClickListener{
            startApp("dvh.SCQR751EM72.iae","io.dcloud.PandoraEntry")
        }

        findViewById<TextView>(R.id.tv_3).setOnClickListener{
            startApp("mkp.UEYI105MN43.suu","io.dcloud.PandoraEntry")
        }

        findViewById<TextView>(R.id.tv_4).setOnClickListener{
            startApp("com.tdx.AndroidNew","com.tdx.Android.TdxActivity")
        }


        findViewById<ImageView>(R.id.imgv_1).setOnClickListener{
           startApp("com.hexin.plat.android","com.hexin.plat.android.LogoEmptyActivity")
        }

        findViewById<ImageView>(R.id.imgv_2).setOnClickListener{
               toPaizhao(this@MainActivity)
        }

        findViewById<ImageView>(R.id.imgv_3).setOnClickListener{
            toXiangce(this@MainActivity)
        }

        findViewById<ImageView>(R.id.imgv_4).setOnClickListener{
            startApp("com.tencent.qqgame","com.tencent.qqgame.login.LogoActivity")
        }

        findViewById<ImageView>(R.id.imgv_5).setOnClickListener{
           startApp("com.eg.android.AlipayGphone","com.eg.android.AlipayGphone.AlipayLogin")
        }

        findViewById<ImageView>(R.id.imgv_6).setOnClickListener{
            startApp("com.ss.android.ugc.aweme","com.ss.android.ugc.aweme.splash.SplashActivity")
        }

        findViewById<ImageView>(R.id.imgv_7).setOnClickListener{
            startApp("com.tencent.mm","com.tencent.mm.ui.LauncherUI")
        }

        findViewById<ImageView>(R.id.imgv_8).setOnClickListener{
            ActivityUtils.startActivity(AppsActivity::class.java)
        }

    }

    fun startWeb(url:String){
        var intent = Intent()
      //  intent.setClass(applicationContext,WebAppActivity::class.java)
        intent.setClass(applicationContext,WebviewActivity::class.java)
        intent.putExtra("url",url)
        ActivityUtils.startActivity(intent)
    }

    fun startApp(pck:String,ac:String){
       try {
           var intent = Intent()
           intent.setComponent(ComponentName( pck,ac))
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
           ActivityUtils.startActivity(intent)
       }catch (e:Exception){
           e.printStackTrace()
           ToastUtils.showShort("APP 没有安装")
       }
    }

    fun toPaizhao(context: Context) {
        context.startActivity(Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA))
    }

    fun toXiangce(context: Context) {
        val packageManager = context.packageManager
        val packageInfos = packageManager.getInstalledPackages(0)
        var packageName: String? = null
        for (i in packageInfos.indices) {
            val temp = packageInfos[i].packageName
            if (temp.contains("gallery") || temp.contains("Gallery")) {
                packageName = temp
                break
            }
        }
        if (!TextUtils.isEmpty(packageName)) {
            var intent = packageManager.getLaunchIntentForPackage(packageName!!)
            if (intent != null) {
                context.startActivity(intent)
            } else {
                intent = Intent(Intent.ACTION_VIEW)
                intent.type = "image/*"
                context.startActivity(intent)
            }
        } else {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.type = "image/*"
            context.startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        enableFullScreen()
        /** 检测新版本 **/
        PgyUpdateManager.Builder()
            .setForced(true)              //设置是否强制更新
            .setUserCanRetry(false)        //失败后是否提示重新下载
            .setDeleteHistroyApk(true)     // 检查更新前是否删除本地历史 Apk
            .register()
    }


    private fun enableFullScreen() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//            // Android 11 及以上版本
//            window.insetsController?.let { controller ->
//                controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
//                controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
//            }
//        } else {
//            // Android 11 以下版本
//            window.decorView.systemUiVisibility = (
//                    View.SYSTEM_UI_FLAG_FULLSCREEN
//                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                    )
//        }
    }
}